#include "kernelanalysisform.h"
#include "ui_kernelanalysisform.h"

kernelAnalysisForm::kernelAnalysisForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::kernelAnalysisForm)
{
    ui->setupUi(this);
    setWindowTitle("核密度分析");
    map=new geoMap();

    connect(ui->fileOpenButton,&QAbstractButton::pressed,this,&kernelAnalysisForm::onFileOpen);
    connect(ui->fileSaveButton,&QAbstractButton::pressed,this,&kernelAnalysisForm::onFileSave);
    connect(ui->confirmButton,&QAbstractButton::pressed,this,&kernelAnalysisForm::onConfirmPressed);
    connect(ui->cancelButton,&QAbstractButton::pressed,this,&kernelAnalysisForm::onCancelPressed);
}

kernelAnalysisForm::~kernelAnalysisForm()
{
    delete ui;
}

void kernelAnalysisForm::onFileOpen()
{
    QString filePath=QFileDialog::getOpenFileName(nullptr,QFileDialog::tr("打开文件"),"",QFileDialog::tr("shp文件 (*.shp)"));
    ui->loadFilePath->setText(filePath);
    //读取点要素
    GDALAllRegister();
    GDALDataset *poDS = static_cast<GDALDataset*>(GDALOpenEx( filePath.toStdString().c_str(), GDAL_OF_VECTOR, nullptr, nullptr, nullptr ));
    if( poDS == nullptr ) exit( 1 );
    int layerNumber = poDS->GetLayerCount();
    for (int i = 0; i<layerNumber ; i++)
    {
        //遍历所有图层
        geoLayer *layer=new geoLayer();
        map->geoLayers.append(layer);

        OGRLayer  *poLayer = poDS->GetLayer( i );
        OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();

        poLayer->ResetReading();
        OGRFeature *poFeature=nullptr;
        while( (poFeature = poLayer->GetNextFeature()) != nullptr )
        {
            //读取几何信息
            OGRGeometry *poGeometry = poFeature->GetGeometryRef();
            GetGeometry geo;
            geo.poGeometry = poGeometry;
            geoObject *newObject;
            if( poGeometry != nullptr)
            {
                auto GeometryType = wkbFlatten(poGeometry->getGeometryType());
                if (GeometryType == wkbPoint)
                {
                    newObject=new geoPoint();
                    newObject->type="Point";
                    double x, y, z;
                    geo.Get_Point(x,y,z);
                    newObject->coordinates.append(QPointF(x,y));
                    newObject->projectedCoordinates.append(QPointF(x,y));
                }
            }

            //读取属性信息
            for( int iField = 0; iField < poFDefn->GetFieldCount(); iField++ )  //遍历所有属性
            {
                OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField );
                switch( poFieldDefn->GetType() )
                {
                case OFTInteger:
                    break;
                case OFTInteger64:
                    break;
                case OFTReal:
                    break;
                case OFTString:
                    break;
                default:
                    break;
                }
            }
            layer->geoObjects.append(newObject);
            OGRFeature::DestroyFeature( poFeature );
        }
    }
    GDALClose( poDS );


}

void kernelAnalysisForm::onFileSave()
{
    savePath=QFileDialog::getSaveFileName(nullptr,QFileDialog::tr("保存文件"),"核密度分析结果",tr("jpg文件 (*.jpg)"));
    ui->saveFilePath->setText(savePath);
}

void kernelAnalysisForm::onConfirmPressed()
{
    if(ui->loadFilePath->text()!=nullptr&&ui->saveFilePath->text()!=nullptr)
    {
        image=new kernelAnalysisImage(map->geoLayers.last(),savePath);
        image->saveOutPut();
        hide();
        delete this;
    }
    else
    {
        QMessageBox msg;
        msg.setText("参数输入不全！");
        msg.setWindowTitle("警告");
        msg.exec();
    }
}

void kernelAnalysisForm::onCancelPressed()
{
    hide();
    delete this;
}
